import {atom,useAtom} from 'jotai';
//定义一个原子状态
const counterAtom = atom(0)
//Create derived atoms with computed values
//使用计算出来的值创建派生的状态
const doubledCountAtom = atom((get) => get(counterAtom) * 2)
function JotaiApp(){
    //使用自定义hook useAtom，允许组件订阅atom的值，当atom的值发生变化时，会让所有的订阅该 atom的组件刷新
    const [counter,setCounter] = useAtom(counterAtom);
    const [doubledCount] = useAtom(doubledCountAtom);
    return (
        <div>
            <p>{counter}</p>
            <p>{doubledCount}</p>
            <button onClick={()=>setCounter(prevState=>prevState+1)}>+</button>
        </div>
    )
}
export default JotaiApp;